package StringC;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-02
 * Time:14:54
 * Text：5. 最长回文子串
 */
public class Text3 {
    public static String longestPalindrome(String s) {
        int left=0,right=0;
        int size=s.length();
        for(int i=0;i<size;i++){
            int a=i-1;
            int b=i+1;
           //奇数情况
            while(a>=0 && b<size){
                if(s.charAt(a)==s.charAt(b)){
                    if(b-a>right-left){
                        right=b;
                        left=a;
                    }
                    a--;
                    b++;
                }else break;
            }
            //偶数情况
            a=i;
            b=i+1;
            while(a>=0 && b<size){
                if(s.charAt(a)==s.charAt(b)){
                    if(b-a>right-left){
                        right=b;
                        left=a;
                    }
                    a--;
                    b++;
                }else break;
            }

        }

        return s.substring(left,right+1);
    }

    public static void main(String[] args) {
        System.out.println(longestPalindrome("a"));
    }
}
